package com.leon.classify.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.leon.classify.domain.CommentDetail;
import com.leon.classify.domain.Company;
import com.leon.classify.domain.FirstClass;
import com.leon.classify.domain.SecondClass;
import com.leon.common.dao.CommonDao;

public class ClassifyDao extends CommonDao {

	/**
	 * @param args
	 */
	public ClassifyDao() {

	}

	/**
	 * 得到所有的一级分类
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<FirstClass> getFirstClassify() {
		List<FirstClass> list = new ArrayList<FirstClass>();
		String hql = "select fc from FirstClass fc order by fc.displayOrder";
		list = super.qryObjectByHql(hql, null);
		if (list != null && list.size() > 0) {
			for (FirstClass fc : list) {
				fc.getSecondClasses().size();
				Set<SecondClass> sec = fc.getSecondClasses();
				if (sec != null && sec.size() > 0) {
					for (SecondClass sc : sec) {
						sc.getCompanies().size();
					}
				}
			}
		}
		return list;
	}
	/**
	 * 得到所有的二级分类
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SecondClass>getSecondClassify(){
		List<SecondClass> list = new ArrayList<SecondClass>();
		String hql="from SecondClass";
		list=super.qryObjectByHql(hql, null);
		if(list!=null && list.size()>0){
			for(SecondClass sc:list){
				sc.getCompanies().size();
			}
		}
		return list;	
	}
	/**
	 * 保存一个FirstClass
	 * 
	 * @param fc
	 */
	public void createClassify(Object obj) {
		super.createObject(obj);
	}

	/**
	 * 删除一个一级分类
	 * 
	 * @param fc
	 */
	public void deleteFirstClassify(String classId) {
		FirstClass fc = this.getFirstClass(classId);
		if (fc.getSecondClasses() != null && fc.getSecondClasses().size() > 0) {
			for (SecondClass sc : fc.getSecondClasses()) {
				this.deleteSecondClassify(sc.getSecClassId());
			}
		}
		super.deleteObject(fc);
	}

	/**
	 * 得到一个一级分类
	 * 
	 * @param classId
	 * @return
	 */
	public FirstClass getFirstClass(String classId) {
		return (FirstClass) super.getObject(FirstClass.class, classId);
	}

	/**
	 * 删除一个二级分类
	 * 
	 * @param fc
	 */
	public void deleteSecondClassify(String classId) {
		SecondClass sc = this.getSecondClass(classId);
		if (sc.getCompanies() != null && sc.getCompanies().size() > 0) {
			super.deleteObjects(sc.getCompanies());
		}
		super.deleteObject(sc);
	}

	/**
	 * 得到一个二级分类
	 * 
	 * @param classId
	 * @return
	 */
	public SecondClass getSecondClass(String classId) {
		return (SecondClass) super.getObject(SecondClass.class, classId);
	}
	/**
	 * 更新一级分类
	 * @param fc
	 */
	public void updateFirstClassify(FirstClass fc){
		super.updateObject(fc);
	}
	public void deleteCompany(String companyId){
		Company c=(Company) super.getObject(Company.class, companyId);
		super.deleteObject(c);
	}
	public Company getCompany(String companyId){
		return (Company) super.getObject(Company.class, companyId);
	}
	//分页的方式取出一个商家的评价
	@SuppressWarnings("unchecked")
	public List <CommentDetail>  getAllCommentDetail(String commentId,int start,int limit){
		List <CommentDetail> cds=new ArrayList<CommentDetail>();
		String hql="from CommentDetail cd where cd.comment.commentId='"+commentId+"' order by cd.commentDate";
		cds=this.qryObjectByHql(hql, null, start, limit);
		return cds;
	}
	@SuppressWarnings("unchecked")
	public List<Company> getSearchDetl(String searchValue){
		List<Company> coms=new ArrayList<Company>();
		String hql="from Company com where com.companyDesc like '%"+searchValue+"%'";
		coms=this.qryObjectByHql(hql, null);
		return coms;
	}
	
}
